package zj.boxuegu.administrator.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import zj.boxuegu.administrator.myapplication.Fragment.MyFragment;
import zj.boxuegu.administrator.myapplication.R;

public class LoginActivity extends AppCompatActivity {
    EditText mUsername,mPassword;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mUsername= (EditText) findViewById(R.id.mUsername);
        mPassword= (EditText) findViewById(R.id.mPassword);
        textView = (TextView) findViewById(R.id.text3);

    }
    public  void  text3(View view){
        Intent intent = new Intent(LoginActivity.this, ZhuCeActivity.class);
        startActivity(intent);
    }
    public void fanhui(View view){
        finish();
    }
    public void logn(View view){
        if(TextUtils.isEmpty(mUsername.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();

        }
      else if(TextUtils.isEmpty(mPassword.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();

       }
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Login");

        params.addQueryStringParameter("username", mUsername.getText().toString());
        params.addQueryStringParameter(" password", mPassword.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


            Gson gson =new  Gson();

                LoginModel loginModel =gson.fromJson(result,LoginModel.class);



           if(loginModel.code==200){
              // Intent intent =new Intent(LoginActivity.this,MainActivity.class);
               //startActivity(intent);



               SharedPreferences sp=getSharedPreferences("login", Context.MODE_PRIVATE);//不让别的进程读到
               SharedPreferences.Editor editor =sp.edit();
               editor.putString("mUsername",mUsername.getText().toString());
               editor.putBoolean("islogin",true);
               editor.commit();


               finish();
           }else{

          }
                Log.i("LoginActivity","onSuccess loginModel code"+loginModel.code);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();



            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    }


